package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.mvp.contract.UpdatePasswordContract;
import com.qf.flash.mvp.presenter.UpdatePasswordPresenter;
import com.qf.flash.mvp.presenter.VerCodePresenter;
import com.qf.flash.utils.AppToast;
import com.qf.flash.utils.MyUtil;
import com.qf.flash.widget.ClearEditText;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.util.RxUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

import static com.qf.flash.constant.FlashCodes.VER_CODE_CHANGE_PASSWORD;

public class ChangPasswordActivity extends BaseActivity implements UpdatePasswordContract.View {

    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_getCode)
    TextView tvGetCode;
    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.et_code)
    ClearEditText etCode;
    @BindView(R.id.et_new_pwd)
    ClearEditText etNewPwd;
    private VerCodePresenter codePresenter;
    private UpdatePasswordPresenter updatePasswordPresenter;
    private int type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chang_password;
    }

    @Override
    protected void initInject() {
        super.initInject();
        codePresenter = new VerCodePresenter();
        updatePasswordPresenter = new UpdatePasswordPresenter();
        initPresenters(codePresenter,updatePasswordPresenter);
    }

    @Override
    protected void initViewAndData() {
        type = getIntent().getIntExtra("type", 0);
        if(type ==0){
            tvTopTitle.setText("修改密码");
        }else if(type ==1){
            tvTopTitle.setText("找回密码");
        }

    }

    public static void start(Activity activity,int type) {
        Intent intent = new Intent(activity, ChangPasswordActivity.class);
        intent.putExtra("type",type);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_sure, R.id.tv_getCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_sure:
                if(etNewPwd.getText().toString().trim().length()>0){
                    if(type==0){
                        updatePasswordPresenter.updatePassword(etCode.getText().toString(),etNewPwd.getText().toString());
                    }else if(type==1) {
                        updatePasswordPresenter.updatePasswordByPhone(etPhone.getText().toString().trim(),etCode.getText().toString(),etNewPwd.getText().toString());
                    }
                }else {
                    AppToast.showToast("密码不能为空！");
                    AppToast.shake(etNewPwd);
                }
                break;
            case R.id.tv_getCode:
                if (MyUtil.isPhone(etPhone.getText().toString().trim())) {
                    if (!isCounting) {
                        startCounting();
                        codePresenter.getVerCode(etPhone.getText().toString().trim(),VER_CODE_CHANGE_PASSWORD);
                    }

                }else {
                    AppToast.showToast("手机号码有误！请填写正确手机号！");
                    AppToast.shake(etPhone);
                }
                break;
        }
    }

    @Override
    public void updateResult(Boolean bool) {
        if(bool){
            AppToast.showToast("---修改密码成功---");
        }

        finish();
    }


    private int interval = 50; //读秒时间
    private boolean isCounting;
    private Disposable disposable;
    private void startCounting() {
        isCounting = true;
        tvGetCode.setTextColor(getResources().getColor(R.color.color_999999));
        disposable = Observable.intervalRange(0, interval + 1, 0, 1, TimeUnit.SECONDS)
                .compose(RxUtils.io_main())
                .subscribe(aLong -> {
                    if (interval == aLong) {
                        tvGetCode.setTextColor(getResources().getColor(R.color.color_333333));
                        tvGetCode.setText("获取验证码");
                        isCounting = false;
                    } else {
                        tvGetCode.setText(getString(R.string.time_left, interval - aLong));
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }

    @Override
    public void onError(ApiException e) {
        if (disposable != null)
            disposable.dispose();
        tvGetCode.setTextColor(getResources().getColor(R.color.color_333333));
        tvGetCode.setText("获取验证码");
        isCounting = false;
    }
}
